# A vector of a color deficient friendly palette with gray:
cdPalette_grey <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
# A vector of a color deficient friendly palette with black:
cdPalette_blk <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")Challenge 2: Spicing things up with ggplot2
For this week’s Challenge, you will have three different options to explore. I’ve arranged these options in terms of their “spiciness,” or the difficulty of completing the task. You are only required to complete one task, but if you are interested in exploring more than one, feel free!
This is a great place to let your creativity show! Make sure to indicate what additional touches you added, and provide any online references you used.
You should create a new Quarto document for this Challenge (make sure to specify self-contained: true, echo: true, and any other formatting specifics you desire in the YAML).
Then, create a setup code chunk to load the packages and read in the surveys.csv file like you did in Lab 2.
Create another code chunk and paste in your code from Question 14 or Question 15 in Lab 2 – we will be modifying the box plot of weights by species!
Alternatively, you can create a copy of your Lab2.qmd file and delete the unnecessary parts.
🌶 Medium: Ridge Plots
In Lab 2, you used side-by-side boxplots to visualize the distribution of weight within each species of rodent. Boxplots have substantial flaws, namely that they disguise distributions with multiple modes.
A “superior” alternative is the density plot. However, ggplot2 does not allow for side-by-side density plots using geom_density(). Instead, we will need to make use of the ggridges package to create side-by-side density (ridge) plots.
For this challenge you are to change your boxplots to ridge plots. You will need to install the
ggridgespackage and explore thegeom_density_ridges()function.
🌶 🌶 Spicy: Exploring Color Themes
The built-in ggplot() color scheme may not include the colors you were looking for. Don’t worry – there are many other color palettes available to use!
You can change the colors used by ggplot() in a few different ways.
Manual Specification
Add the scale_color_manual() or scale_fill_manual() functions to your plot and directly specify the colors you want to use. You can either:
define a vector of colors within the
scalefunctions (e.g.values = c("blue", "black", "red", "green")) ORcreate a vector of colors using hex numbers and store that vector as a variable. Then, call that vector in the
scale_color_manual()function.
Here are some exaplme hex color schemes:
If you are interested in using specific hex colors, I like the image color picker app to find the colors I want.
Package Specification
Install a package and use its available color scale. Popular options include:
RColorBrewer– change colors by usingscale_fill_brewer()orscale_colour_brewer().viridis– change colors by usingscale_colour_viridis_d()for discrete data,scale_colour_viridis_c()for continuous data.ggsci– change colors by usingscale_color_<PALNAME>()orscale_fill_<PALNAME>(), where you specify the name of the palette you wish to use (e.g.scale_color_aaas()).
This website provides an exhaustive list of color themes available through various packages.
In this challenge you are expected to use this information to modify the boxplots you created Lab 2. First, you are to color the boxplots based on the variable
genus. Next, you are to change the colors used forgenususing either manual color specification or any of the packages listed here (or others!).
🌶 🌶 🌶 Hot: Exploring ggplot2 Annotation
Some data scientists advocate that we should try to eliminate legends from our plots to make them more clear. Instead of legends, we should use annotation.
We can add annotation to a ggplot() using the annotate() function.
Note that I’ve labeled the “Sigmodon” and “Perognathus” genera, so the reader can tell that these boxplots are associated with their respective genus.
In this challenge you are expected to use this information to modify the boxplots you created in Lab 2. First, you are to color the boxplots based on the variable
genus. Next, you are to add annotations for each genus next to the boxplot(s) associated with that genus. Finally, you are expected to use thetheme()function to remove the color legend from the plot, since it is no longer needed!